package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;


public class Client extends Thread {
	Socket socket = null;
	Socket socket2 = null;
	int clientnum = 0;
	
	// infomation
	String username;
	int sex;
	int grade;
	int islogin=1;
	ArrayList<String> friendList;
	String place;
	String groupName;
	
	// settings
	boolean logon = false; // important! when login, set true
	int canHelp = 1;
	int canSeen = 1;
	int canDisturb = 1; // default : all can
	
	BufferedReader is = null;
	PrintWriter os = null;
	BufferedReader is2 = null;
	PrintWriter os2 = null;
	boolean running = true;
	public Client(Socket socket,Socket socket2,int clientnum){
		this.socket = socket;
		this.socket2=socket2;
		this.clientnum = clientnum;
		friendList = new ArrayList<String>();
	}
	
	public synchronized void run(){
		
		try {
			is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			os = new PrintWriter(socket.getOutputStream());
		    is2 = new BufferedReader(new InputStreamReader(socket2.getInputStream()));
			os2 = new PrintWriter(socket2.getOutputStream());
			while (running){
				    String request = is.readLine();	
				    System.out.println("*****************************");
					System.out.println("request="+request);
					analyze(request);
				
			}
			
			os.close();
			is.close();
			socket.close();
			os2.close();
			is2.close();
			socket2.close();
			
		} catch (IOException e) {
			e.printStackTrace();			
		}
	}
	
	private String fromListToString (ArrayList<String> a){
		String out ="";
		for (int i = 0; i < a.size(); i++){
			out += ";" + a.get(i);
		}
		
		return out.substring(1);
	}
	
	private String getGnameByLocation(){
		return Global.loca_gname;
	}

	private void analyze(String request) {
		// TODO Auto-generated method stub
		String []args = request.split("\\s+");
		if (args[0].equals("logon")){
			String user = args[1];
			String passwd = args[2];
			
			if(Global.clients.containsKey(user))
			{
				System.out.println("---the user logon  already-----");
				os.println(Global.ALREADYLOGON);
				os.flush();
			}
			// if username is not existed
			else if (!Global.serverDAO.isNameUsed(user)){
				System.out.println("-----the user" +args[1]+"not exist-----");
				os.println("2");
				os.flush();
				this.running=false;
			}
			else{
				if (!Global.serverDAO.isPasswdRight(user, passwd))
				{
					System.out.println("-----the user" +args[1]+" password is not right-----");
					os.println("2");
					os.flush();
				}
				else
				{
					Global.clients.put(user,this);
					this.logon = true;
					this.username = user;
					Global.serverDAO.setIsLogin(user, 1);
					this.sex = Global.serverDAO.getSexByName(user);
					this.grade = Global.serverDAO.getGradeByName(user)+10;
					Global.serverDAO.setGrade(user, this.grade);
					this.canHelp = Global.serverDAO.getCanHelp(user);
					this.canSeen = Global.serverDAO.getCanSeen(user);
					this.canDisturb = Global.serverDAO.getCanDisturb(user);
					System.out.println("-----the user "+args[1]+" logon success------");
					String ans = ((Integer)Global.RIGHT).toString();
					if (null != Global.serverDAO.getFriendListByName(user) && !Global.serverDAO.getFriendListByName(user).isEmpty()){
						System.out.println("the user's friend list:"+Global.serverDAO.getFriendListByName(user));
						String []fris = Global.serverDAO.getFriendListByName(user).split(";");	
						ans += " "+this.sex+" "+this.grade
						        +" "+Global.serverDAO.getFriendListByName(user)
								+" "+Global.serverDAO.getFriendSexByName(user)
								+" "+Global.serverDAO.getFriendLoginByName(user)
								+" "+Global.serverDAO.getFriendPosByName(user)
								+" "+Global.serverDAO.getFriendGradeByName(user)
								+" "+Global.serverDAO.getFriendGnameByName(user)
								+" "+getGnameByLocation();
						System.out.println("the personal infomation="+ans);
						if (canSeen == 0 || canSeen == 1 || canSeen == 5){
							for (String s : fris){
								friendList.add(s);
								Client c = Global.clients.get(s);
								if (c != null){
									c.os2.println("FredLogOn "+user);
									c.os2.flush();
									System.out.println("send the "+user+" logon inforamtion  to "+c.username);
								}
							}
						}			
						else{
							for (String s : fris){
								friendList.add(s);
							}
						}
					}
					else
					{
						ans += " "+this.sex+" "+this.grade
				        +" "+null
						+" "+null
						+" "+null
						+" "+null
						+" "+null
						+" "+getGnameByLocation();
					}
					os.println(ans);
					os.flush();	
				}//else
								
			}
			System.out.println("**********end***************");
		}
		else if (args[0].equals("nuser")){
			this.username = args[1];
			System.out.println("-----the user " +this.username+" want to register-----");
			if (Global.serverDAO.isNameUsed(this.username)){
				os.println(Global.USERNAMEALREADYEXIST);
				os.flush();
			}
			else{
				os.println(Global.RIGHT);
				os.flush();
			}
			System.out.println("**********end***************");
		}
		else if (args[0].equals("npasswd")){
			this.username = args[1];
			this.logon = true;
			Global.clients.put(this.username,this);
			this.sex = Integer.parseInt(args[3]);;
			this.grade = 10;
			Global.serverDAO.insert(username, Integer.parseInt(args[3]),args[2]);
			Global.serverDAO.setIsLogin(this.username, 1);
			Global.serverDAO.setGrade(this.username, this.grade);
			this.canHelp = Global.serverDAO.getCanHelp(this.username);
			this.canSeen = Global.serverDAO.getCanSeen(this.username);
			this.canDisturb = Global.serverDAO.getCanDisturb(this.username);
			System.out.println("-----the user "+args[1]+" register success------");
			
			    String ans = ((Integer)Global.RIGHT).toString();
				ans += " "+sex+" "+grade+" "+getGnameByLocation();
				System.out.println("the personal infomation="+ans);
				os.println(ans);
				os.flush();
				System.out.println("**********end***************");
			}
		else if (args[0].equals("checkIN"))
		{
			//refresh its place info and inform all his friends!
			this.place = args[1];
			this.groupName = args[3];
			String command = "FredCheckIn "+this.username+" "+this.place;
			Global.serverDAO.setPosition(this.username, this.place);	
			Global.serverDAO.setGroupName(this.username,this.groupName);	
			//send the user check in infomation to it's friend
			System.out.println(this.username+" friendList:"+friendList.toString());
			if(this.friendList.size()>0)
			{
				for (String friendname : friendList)
				{
					Client c = Global.clients.get(friendname);
					if(c!=null)
					{
						c.os2.println(command);
						c.os2.flush();
						System.out.println("send the "+this.username+" checkIN inforamtion  to "+c.username);
					}
			    }	
			}
			//if the user is the first in the location, then he is responsible for creating a group
			//also it is need to send the newGroup information to all
			if (!Global.groups.containsKey(this.place))
			{
				System.out.println("群组是一个新建的群组");
			    Group newGroup = new Group(this.place,this.username,0,this.groupName);
			    //gname  Group
				Global.groups.put(this.place,newGroup);
				System.out.println(Global.groups.keySet().toString());
				// gname  location map
				Global.GnameLocMap.put(this.groupName, this.place);
				// location - gname map
				Global.LocGnameMap.put(this.place, this.groupName);
			    System.out.println("需要更新服务器保存的loca_gname");
			    System.out.println("before loca_gname="+Global.loca_gname);
				String gnames[]=Global.loca_gname.split(";");
				Global.loca_gname="";
				String poss[]=Global.location.split(";");
				for(int i=0;i<gnames.length;i++)
				{
					if(poss[i].endsWith(this.place))
					   gnames[i]=this.groupName;
					Global.loca_gname+=";"+gnames[i];
					
				}
				Global.loca_gname=Global.loca_gname.substring(1);
				 System.out.println("end loca_gname="+Global.loca_gname);
				for (String s : Global.clients.keySet()){
					Client c = Global.clients.get(s);
					c.os2.println("newGroup "+this.groupName+" "+this.place);
					c.os2.flush();			
					}

				os.println(Global.RIGHT);
				os.flush();
			}// if the group already exsit
			else 
			{
				String nameList="";
				String sexList="";
				String gradeList="";
				String gnameList="";
				for (String s : Global.groups.get(this.place).peopleList)
				{
					Client c = Global.clients.get(s);
					c.os2.println("GroupMemLogOn"+" "+this.username+" "+this.sex+" "+this.islogin+" "+this.place+" "+this.grade);
					c.os2.flush();
					nameList+=";"+c.username;
					sexList+=";"+c.sex;	
					gradeList+=";"+c.grade;
					gnameList+=";"+c.groupName;
				}
				Global.groups.get(this.place).peopleList.add(this.username);
				nameList=nameList.substring(1);
				sexList=sexList.substring(1);
				gradeList=gradeList.substring(1);
				gnameList=gnameList.substring(1);
				os.println(Global.RIGHT+" "+nameList+" "+sexList+" "+gradeList+" "+gnameList);
				os.flush();
			}
			System.out.println("**********end***************");
		}
		else if (args[0].equals("checkOFF")){
			if (this.groupName!= null)
			{
				   Global.groups.get(this.place).peopleList.remove(this.username);
				   Global.serverDAO.setPosition(this.username,null);
				   Global.serverDAO.setGroupName(this.username, "0");
			      //将用户checkOFF的消息发送给同群组的成员（除了自己）
				   System.out.println("the user's groupMem list:"+Global.serverDAO.getGroupMemByGroupName(this.groupName));
				   if(null!=Global.serverDAO.getGroupMemByGroupName(this.groupName)){
					   String []gm = Global.serverDAO.getGroupMemByGroupName(this.groupName).split(";");	
					   String command ="GroupMemLogOff"+"\t"+this.username;
					   for (String s : gm)
					   {
						   Client c = Global.clients.get(s);
						   if (c != null && !c.username.equals(this.username)){
							   c.os2.println(command);
							   c.os2.flush();
							   System.out.println("send the "+this.username+" GroupMemLogOff inforamtion  to "+c.username);
						   }
					   }
				   }
				   if(Global.groups.get(this.place).peopleList.isEmpty())
				   {
					for (String s : Global.clients.keySet()){
						Client c = Global.clients.get(s);
						c.os2.println("delGroup "+this.groupName+" "+this.place);
						c.os2.flush();							
						}
					    System.out.println("需要更新服务器保存的loca_gname");
					    System.out.println("before loca_gname="+Global.loca_gname);
						String gnames[]=Global.loca_gname.split(";");
						Global.loca_gname="";
						String poss[]=Global.location.split(";");
						for(int i=0;i<gnames.length;i++)
						{
							if(poss[i].endsWith(this.place))
							   gnames[i]="0";
							Global.loca_gname+=";"+gnames[i];
							
						}
						Global.loca_gname=Global.loca_gname.substring(1);
						 System.out.println("end loca_gname="+Global.loca_gname);
					Global.groups.remove(this.place);
					Global.GnameLocMap.put(this.groupName,"0");
					Global.LocGnameMap.put(this.place,"0");
				   }
					//用户CheckOFF消息发送给好友
					if (this.friendList.size()>0){
						System.out.println("the user's friend list:"+this.friendList);
						for(int i=0;i<this.friendList.size();i++)
						{
								Client c = Global.clients.get(this.friendList.get(i));
								if (c != null){
									c.os2.println("FredCheckOFF "+this.username);
									c.os2.flush();
									System.out.println("send the "+this.username+" check off inforamtion  to "+c.username);
								}
							}
						}
					
			}
			this.groupName ="0";
			this.place=null;
			os.println(Global.RIGHT);
			os.flush();
			System.out.println("**********end***************");
		}
		else if (args[0].equals("helpCheckIN")){
			//0  for all
			//1  only for friend
			//2  only for user in the same group
			//3  for no
		    //4 need query
		    //5 for friend and people in the same group 
			String userto = args[1];			
			int level = Global.serverDAO.getCanHelp(userto); 
			Client c = Global.clients.get(userto);
			if (null != c){
				String []friends = Global.serverDAO.getFriendListByName(userto).split(";");
				boolean isFriend = false;
				for (String fri : friends)
				{
					if (fri.equals(this.username)){
						isFriend = true;
						break;
					}
				}
				System.out.println(c.username +" " +this.username+" isFred="+isFriend);
				switch(level){
					case 0:
						//userto checkin
						os.println(Global.RIGHT);
						os.flush();
						c.place = this.place;
						c.groupName=this.groupName;
						Global.serverDAO.setPosition(c.username,c.place);
						Global.serverDAO.setGroupName(c.username,c.groupName);
						for (String fri : friends){
							Client f = Global.clients.get(fri);
							if(f!=null){
								f.os2.println("FredCheckIn "+userto + " "+this.place+" "+ this.groupName);
								f.os2.flush();
							}
						}
						break;
					case 1:
						//if userfrom is the friend of userto
						if (isFriend){
							os.println(Global.RIGHT);
							os.flush();
							c.place = this.place;
							c.groupName=this.groupName;
							Global.serverDAO.setPosition(c.username,c.place);
							Global.serverDAO.setGroupName(c.username,c.groupName);
							for (String fri : friends){
								Client f = Global.clients.get(fri);
								if(f!=null){
									f.os2.println("FredCheckIn "+userto + " "+this.place+" "+ this.groupName);
									f.os2.flush();
								}
							}
						}
						else
						{
							os.println(Global.AUTHORITYERR);
							os.flush();
						}
						break;
					case 2:
						os.println(Global.RIGHT);
						os.flush();
						break;
					case 3:
						/* nothing */
						os.println(Global.AUTHORITYERR);
						os.flush();
						break;
					case 4:
						c.os2.println("beHelpCheckIN "+place+" "+this.username+" "+userto);
						try {
							String res = is.readLine(); 				
							if (res.equalsIgnoreCase("YES")){
								os.println(Global.RIGHT);
								os.flush();
								c.place = this.place;
								c.groupName=this.groupName;
								Global.serverDAO.setPosition(c.username,c.place);
								Global.serverDAO.setGroupName(c.username,c.groupName);
								for (String fri : friends){
									Client f = Global.clients.get(fri);
									if(f!=null){
										f.os2.println("FredCheckIn "+userto + " "+this.place+" "+ this.groupName);
										f.os2.flush();
									}
								}
							}
							else
							{
								os.println(Global.AUTHORITYERR);
								os.flush();
							}
						
						} catch (IOException e) {
							e.printStackTrace();
						}
						break;
					case 5:
						if (isFriend){
							os.println(Global.RIGHT);
							os.flush();
							c.place = this.place;
							c.groupName=this.groupName;
							Global.serverDAO.setPosition(c.username,c.place);
							Global.serverDAO.setGroupName(c.username,c.groupName);
							for (String fri : friends){
								Client f = Global.clients.get(fri);
								if(f!=null){
									f.os2.println("FredCheckIn "+userto + " "+this.place+" "+ this.groupName);
									f.os2.flush();
								}
							}
						}
						else
						{
							os.println(Global.AUTHORITYERR);
							os.flush();
						}
						break;
						
				}//switch
			}//if c!= null
			else{
				os.println(Global.RECERIVERNOTLOGON);
				os.flush();
			}
			
		}
		else if (args[0].equals("AddFred")){
			String userto = args[1];
			String userfrom = args[2];
			String userfromsex = args[3];
			String userfromislogin =args[4];
			String userfromposition=args[5];
			String userfromgrade=args[6];
			String userfromgname=args[7];
			Client c = Global.clients.get(userto);
			if (c == null){
				os.println(Global.RECERIVERNOTLOGON);
				os.flush();
			}else
			{
			if (c.friendList.contains(userfrom)){
				os.println(Global.ALREADYFRIEND);
				os.flush();
			}
			else{
				try {
					c.os2.println("beFred "+userfrom+"\t"+userfromsex+"\t"+userfromislogin+"\t"+userfromposition+"\t"+userfromgrade+" "+userfromgname);
					c.os2.flush();
					String res = c.is2.readLine();
					System.out.println("被添加好友的用户"+userto+"的回复是 "+ res);
					if (res.equalsIgnoreCase("YES")){
						/*update data in memory*/
						c.friendList.add(userfrom);
						friendList.add(userto);
						/*update database*/
						Global.serverDAO.setFriendList(userfrom, fromListToString(friendList));
						Global.serverDAO.setFriendList(userto, fromListToString(c.friendList));
						String command = Global.RIGHT +" "+c.sex+" "+c.islogin+" "+" "+c.place+" "+c.grade+" "+c.groupName;				
						os.println(command);
						os.flush();
					}
					else{
						os.println(Global.AUTHORITYERR);
						os.flush();
					}
				} catch (IOException e) {e.printStackTrace();}
				
			}
			}//else c != null
		}
		else if (args[0].equals("chat")){
			String userto = args[1];
			String userfrom = args[2];
			String content = args[3];
			for (int i = 4; i < args.length; i++){
				content += " "+args[i];
			}
			System.out.println("chat content : "+ content);
			Client c = Global.clients.get(userto);
			if (c == null){
				os.println(Global.RECERIVERNOTLOGON);
				os.flush();
			}else{
			boolean isFriend = false;
			boolean isSameGroup = false;
			if (c.friendList.contains(userfrom)) isFriend = true;
			if (c.groupName == null || c.groupName.equals(""))
				isSameGroup = false;
			else{
				if (c.groupName.equals(groupName))
					isSameGroup = true;
			}
			switch(c.canDisturb){
				case 0:
					/*all can disturb*/
					os.println(Global.RIGHT);
					os.flush();
					c.os2.println("chat "+userto+" "+userfrom+" "+content);
					c.os2.flush();
					break;
				case 1:
					/*only friend can disturb*/
					if (isFriend){
						os.println(Global.RIGHT);
						os.flush();
						c.os2.println("chat "+userto+" "+userfrom+" "+content);
						c.os2.flush();
					}
					else{
						os.println(Global.AUTHORITYERR);
						os.flush();
					}
					break;
				case 2:
					/*only users in the same group can*/
					if (isSameGroup){
						os.println(Global.RIGHT);
						os.flush();
						c.os2.println("chat "+userto+" "+userfrom+" "+content);
						c.os2.flush();
					}
					else{
						os.println(Global.AUTHORITYERR);
						os.flush();
					}		                                   
					break;
				case 3: /*for no*/
					os.println(Global.AUTHORITYERR);
					os.flush();
					break;
				case 4:
					os.println(Global.RIGHT);
					os.flush();
					break;
				case 5:
					if(isFriend || isSameGroup){
						os.println(Global.RIGHT);
						os.flush();
						c.os2.println("chat "+userto+" "+userfrom+" "+content);
						c.os2.flush();
					}
					else{
						os.println(Global.AUTHORITYERR);
						os.flush();
					}
					break;
					
			}//switch
			}//else
		}
		else if (args[0].equals("Act")){
			String userto = args[1];
			String userfrom = args[2];
			int actnum = Integer.parseInt(args[3]);
			Client c = Global.clients.get(userto);
			if (c == null){
				os.println(Global.RECERIVERNOTLOGON);
				os.flush();
			}else{
			boolean isFriend = false;
			boolean isSameGroup = false;
			if (c.friendList.contains(userfrom)) isFriend = true;
			if (c.groupName == null || c.groupName.equals(""))
				isSameGroup = false;
			else{
				if (c.groupName.equals(groupName))
					isSameGroup = true;
			}
			switch(c.canDisturb){
				case 0:
					/*all can disturb*/
					os.println(Global.RIGHT);
					os.flush();
					c.os2.println("Act "+userto+" "+userfrom+" "+actnum);
					c.os2.flush();
					break;
				case 1:
					/*only friends can disturb*/
					if (isFriend){
						os.println(Global.RIGHT);
						os.flush();
						c.os2.println("Act "+userto+" "+userfrom+" "+actnum);
						c.os2.flush();
					}
					else{
						os.println(Global.AUTHORITYERR);
						os.flush();
					}
					break;
				case 2:
					/*only in the same group can disturb*/
					if (isSameGroup){
						os.println(Global.RIGHT);
						os.flush();
						c.os2.println("Act "+userto+" "+userfrom+" "+actnum);
						c.os2.flush();
					}
					else{
						os.println(Global.AUTHORITYERR);
						os.flush();
					}
					break;
				case 3:
					os.println(Global.AUTHORITYERR);
					os.flush();
					break;
				case 4:
					os.println(Global.RIGHT);
					os.flush();
					break;
//					c.os2.println("reqAct "+userto + " "+userfrom);
//					c.os2.flush();
//					String res;
//					try {
//						res = is.readLine();
//						if (res.equalsIgnoreCase("YES")){
//							os.println(Global.RIGHT);
//							os.flush();
//							c.os2.println("chat "+userto+" "+userfrom+" "+actnum);
//							c.os2.flush();
//						}
//						else{
//							os.println(Global.AUTHORITYERR);
//							os.flush();
//						}
//					} catch (IOException e) {e.printStackTrace();}
				case 5:
					if(isFriend || isSameGroup){
						os.println(Global.RIGHT);
						os.flush();
						c.os2.println("chat "+userto+" "+userfrom+" "+actnum);
						c.os2.flush();
					}
					else{
						os.println(Global.AUTHORITYERR);
						os.flush();
					}
					break;
					
			}
			}//else
		}
		else if (args[0].equals("Gchat")){
			String gname = args[1];
			String userfrom = args[2];
			String content = args[3];
			String []gmember = Global.serverDAO.getGroupMemByGroupName(gname).split(";");
			//userfrom is not in the group
			if (groupName == null || !groupName.equals(gname)){
				os.println(Global.AUTHORITYERR);
				os.flush();
			}
			else{
				// send msg to every member in the group
				for (String gm : gmember){
					Client c = Global.clients.get(gm);
					if(!c.username.equals(this.username)){
					 c.os2.println("Gchat "+groupName+" "+userfrom +" "+content);
					 c.os2.flush();
					}
				}
				os.println(Global.RIGHT);
				os.flush();
				
			}
				
		}
//		else if (args[0].equals("FriendList")){
//			String userfrom = args[1];
//			String friends = fromListToString(friendList);
//			
//			os.println(Global.RIGHT + " " +friends);
//			os.flush();
//			
//		}
//		else if (args[0].equals("Group")){
//			String userfrom = args[1];
//			//?????when to initial the server's grouplist???
//			String []loc = Global.location.split(";");
//			String gn = Global.LocGnameMap.get(loc[0]);
//			for (int i = 1; i < loc.length; i++){
//				gn += ";" + Global.LocGnameMap.get(loc[i]);
//			}
//			os.println(gn);
//			os.flush();
//			
//		}
//		else if (args[0].equals("MyGroup")){
//			/*add into group*/
//			String userfrom = args[1];
//			String gname = args[2];
//			groupName = gname;
//			//String gmember = Global.serverDAO.getGroupMemByGroupName(gname);
//			
//			/*refresh database*/
//			Global.serverDAO.setGroupName(userfrom, gname);
//			
//			
//			/*inform every member in the group*/
//			for (String n : Global.groups.get(gname).peopleList){
//				Client c = Global.clients.get(n);
//				c.os2.println("addGroupMem "+gname + " " + userfrom);
//				c.os2.flush();
//			}
//			
//			
//			/*refresh Global.groups*/
//			if (Global.groups.get(gname).peopleNum() == 0) Global.groups.get(gname).setLeaderName(userfrom);
//			Global.groups.get(gname).add(userfrom);	
//	
//		}
		else if (args[0].equals("SetSeen")){
			String userfrom = args[1];
			int limit = Integer.parseInt(args[2]);
			
			/*refresh database*/
			Global.serverDAO.setCanSeen(userfrom, limit);
			
			/*refresh server*/
			canSeen = limit;			
			os.println(Global.RIGHT);
			os.flush();
			
		}
		else if (args[0].equals("SetDisturb")){
			
			String userfrom = args[1];
			int limit = Integer.parseInt(args[2]);
			
			/*refresh database*/
			Global.serverDAO.setCanDisturb(userfrom, limit);
			
			/*refresh server*/
			canDisturb = limit;
			os.println(Global.RIGHT);
			os.flush();
			
		}
		else if (args[0].equals("SetHelp")){
			String userfrom = args[1];
			int limit = Integer.parseInt(args[2]);
			
			/*refresh database*/
			Global.serverDAO.setCanHelp(userfrom, limit);
			
			/*refresh server*/
			canHelp = limit;			
			os.println(Global.RIGHT);
			os.flush();
		}
		else if (args[0].equals("logoff")){
			String userfrom = args[1];
			Global.serverDAO.setIsLogin(userfrom,0);
			//将用户logoff的消息发送给所有的好友
			if (null != Global.serverDAO.getFriendListByName(userfrom))
			{
				System.out.println("the user's friend list:"+Global.serverDAO.getFriendListByName(userfrom));
				String []fris = Global.serverDAO.getFriendListByName(userfrom).split(";");	
                String command ="FredLogOff"+"\t"+userfrom;
				os.flush();
					for (String s : fris)
					{
						Client c = Global.clients.get(s);
						if (c != null){
							c.os2.println(command);
							c.os2.flush();
							System.out.println("send the "+userfrom+" FredLogOff inforamtion  to "+c.username);
						}
					}
			}
			//将用户logoff的消息发送给所有的同群组成员，默认做的也是checkOFF的操作
			if (this.groupName!= null && null != Global.serverDAO.getGroupMemByGroupName(this.groupName))
			{
				System.out.println("the user's friend list:"+Global.serverDAO.getFriendListByName(userfrom));
				String []gm = Global.serverDAO.getGroupMemByGroupName(this.groupName).split(";");	
                String command ="GroupMemLogOff"+"\t"+userfrom;
				os.flush();
					for (String s : gm)
					{
						Client c = Global.clients.get(s);
						if (c != null){
							c.os2.println(command);
							c.os2.flush();
							System.out.println("send the "+userfrom+" GroupMemLogOff inforamtion  to "+c.username);
						}
					}
					Global.groups.get(this.place).peopleList.remove(this.username);
			}
			Global.serverDAO.setGroupName(username,"0");
			Global.serverDAO.setIsLogin(userfrom, 0);
			Global.serverDAO.setPosition(username, null);
			os.println(Global.RIGHT);
			os.flush();
			try {
				System.out.println("服务器段关闭与该客户端的通信"+this.username);
			} catch (Exception e) {
				e.printStackTrace();
			};
			Global.clients.remove(this.username);
			System.out.println(Global.clients.keySet().toString());
			this.running=false;
		}//end of elseif
		else
		{
			System.out.println("无效的命令");
			os.println(2);
			os.flush();
		}
	}//end of func
}//end of class
